﻿using System.Collections.Generic;
using System.Data.SQLite;
using System.Data;
using WpfApplication5.Models;

namespace WpfApplication5
{
    public class SQLiteDatabase
    {
        private readonly SQLiteHelper _db;
        public SQLiteDatabase(SQLiteHelper db)
        {
            this._db = db;
        }

        public List<FunctionCategory> ParentLoad(int id =0)
        {
            List<FunctionCategory> ltResult = null;
            string QueryStr = "select * from functionCat where pid = @pid order by ord";
            var dt = _db.ExecuteDataTable(QueryStr, new SQLiteParameter("@pid", id));
            if (dt != null)
            {
                ltResult = new List<FunctionCategory>();
                foreach (DataRow row in dt.Rows)
                {
                    var bean = new FunctionCategory(row);
                    ltResult.Add(bean);
                }
            }
            return ltResult;
        }

        public List<FuncationInfo> GetFunctionInfo(int categoryId)
        {
            List<FuncationInfo> ltInfo = null;
            var dt = _db.ExecuteDataTable("select * from functionInfo where cat_id = @cat_id order by code",
                new SQLiteParameter("@cat_id", categoryId));
            if (dt != null)
            {
                ltInfo = new List<FuncationInfo>();
                foreach (DataRow row in dt.Rows)
                {
                    var bean = new FuncationInfo(row);
                    ltInfo.Add(bean);
                }
            }
            return ltInfo;
        }

        public List<FunctionParameter> GetParameters(string id)
        {
            List<FunctionParameter> ltResult = null;
            var dt = _db.ExecuteDataTable("Select * from functionParams where fid = @fid order by name",
                new SQLiteParameter("@fid", id));
            if (dt != null)
            {
                ltResult = new List<FunctionParameter>();
                foreach (DataRow row in dt.Rows)
                {
                    var bean = new FunctionParameter(row);
                    ltResult.Add(bean);
                }
            }
            return ltResult;
        }
    }
}
